﻿#include "BrowserTableHandlerNCTLayUp.h"
#include "OperatorsInterface/OperAbstractNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTRegularTrimLayer.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QPushButton>
namespace GUI
{
    void BrowserTableHandlerNCTLayUp::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* mgr = phys->getFITKAcousticsNCTManager();
        if (mgr == nullptr) return;
        _maxlayers = 0;
        QList<Acoustics::FITKAcousticsAbsNCT*>  list = mgr->getAcousticsNCTByType(
            Acoustics::FITKAcousticsAbsNCT::NCTType::TreatmentLayUp);
        for (int i = 0; i < list.size(); ++i)
        {
            //转换为多层包
            Acoustics::FITKAcousticsNCTLayUp* nct = dynamic_cast<Acoustics::FITKAcousticsNCTLayUp*>(list.at(i));
            if (nct == nullptr) continue;
            //记录最大层数
            int layers = nct->getLayCount();
            if (layers > _maxlayers)
                _maxlayers = layers;
        }

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Layers");
        for (int i = 0; i < _maxlayers; ++i)
        {
            QString Layer = QString("Layer-%1").arg(i + 1);
            headerLabels << Layer;
        }
        headerLabels << tr("Color") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }
    void BrowserTableHandlerNCTLayUp::updateData()
    {
        if (_tableWidget == nullptr) return;
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* mgr = phys->getFITKAcousticsNCTManager();
        if (mgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsNCT*>  list = mgr->getAcousticsNCTByType(
            Acoustics::FITKAcousticsAbsNCT::NCTType::TreatmentLayUp);
        //多层包数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为多层包
            Acoustics::FITKAcousticsNCTLayUp* nct = dynamic_cast<Acoustics::FITKAcousticsNCTLayUp*>(list.at(i));
            if (nct == nullptr) continue;
            //数据ID
            const int id = nct->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(nct->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(nct->getLayCount()), id));
            //填充每一层的属性
            for (int j = 0; j < nct->getLayCount(); ++j)
            {
                auto layerData = nct->getDataByIndex(j);
                QString layer1 = getLayer(layerData);
                _tableWidget->setItem(i, 1 + j + 1, new QTableWidgetItem(layer1, id));
            }
            //颜色 NOTES
            float color[3] = { 0,0,0 };
            nct->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i, 1 + _maxlayers + 1, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button, nct); });

            //_tableWidget->setItem(i, 1 + _maxlayers + 2, new QTableWidgetItem(nct->getDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractNCT * BrowserTableHandlerNCTLayUp::getNCTOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractNCT>("OperNCTLayUp");
    }

    bool BrowserTableHandlerNCTLayUp::enableAttriSet()
    {
        //多层包没有批量处理
        return false;
    }

    QString BrowserTableHandlerNCTLayUp::getLayer(Acoustics::FITKAcousticsNCTAbstractLayObj *layerData)
    {
        if (!layerData) return QString();
        auto type = layerData->getNCTLayUpType();     
        switch (type)
        {
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam:
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber:
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap:
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel: {
            // 泡沫、缝隙、实体和纤维类型
            Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
            if (phys == nullptr) return QString();
            Acoustics::FITKAcousticsMaterialManager* mgr = phys->getMaterialManager();
            if (mgr == nullptr) return QString();
            auto layer = dynamic_cast<Acoustics::FITKAcousticsNCTRegularTrimLayer*>(layerData);
            if(!layer)return QString();
            if (type != Acoustics::FITKAcousticsNCTAbstractLayObj::Gap) {
                Interface::FITKAbstractMaterial* mat = mgr->getDataByID(layer->getSolidMaterialID());
                if(!mat)return QString();
                return mat->getDataObjectName();
            }else {
                Interface::FITKAbstractMaterial* mat = mgr->getDataByID(layer->getFluidMaterialID());
                if (!mat)return QString();
                return mat->getDataObjectName();
            }
        }
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Septum: 
            // 隔离类型
            return "Septum";
        case Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Perforated: 
            // 多孔类型
            return "Perforated";
        case Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Resistive: 
            // 阻尼类型
            return "Resistive";
        default:
            break;
        }
        return QString();
    }
}